package com.ccbooks.adapter;

import java.util.List;

import com.ccbooks.listener.DelListener;
import com.ccbooks.listener.OpenBookListerer;
import com.ccbooks.util.BookUtil;
import com.ccbooks.view.BookShelfView;
import com.ccbooks.view.R;
import com.ccbooks.vo.Book;

import android.accounts.OnAccountsUpdateListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ShelfFloorAdapter extends BaseAdapter {

	private BookShelfView context;
	public List<Book> books;
	private LayoutInflater inflater;
	public int orientation; // 判断横竖屏 1 竖屏 ;2横屏
	// public boolean isEdit;

	public int count;

	// int[] arrint=
	// {R.drawable.book1,R.drawable.book2,R.drawable.book3,R.drawable.book4,R.drawable.book5,R.drawable.book6};

	public ShelfFloorAdapter(BookShelfView context, List<Book> books) {
		this.context = context;
		this.books = books;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		if (orientation != 2) {
			if (count > 5) {
				return count;
			} else if ((BookUtil.bookSize(books) + 2) / 3 < 5) {
				return 5;
			} else {
				return (BookUtil.bookSize(books) + 2) / 3;
			}
		} else {
			if (count > 3) {
				return count;
			} else if ((BookUtil.bookSize(books) + 5) / 6 < 3) {
				return 3;
			} else {
				return (BookUtil.bookSize(books) + 5) / 6;
			}
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view;
		
		if (orientation == 2) {
			view = showInHorizontal(position);
		} else {
			view = showInVertical(position);
		}
		return view;
	}

	/** ������ʾ */
	private View showInVertical(int position) {
		LinearLayout ly = (LinearLayout) inflater.inflate(R.layout.shelffloor,
				null);
		// ��ø��ֿؼ�;
		Button btBook1 = (Button) ly.findViewById(R.id.btBook1);
		Button btBook2 = (Button) ly.findViewById(R.id.btBook2);
		Button btBook3 = (Button) ly.findViewById(R.id.btBook3);
		Button btBook4 = (Button) ly.findViewById(R.id.btBook4);
		ImageView ivdel1 = (ImageView) ly.findViewById(R.id.ivdel1);
		ImageView ivdel2 = (ImageView) ly.findViewById(R.id.ivdel2);
		ImageView ivdel3 = (ImageView) ly.findViewById(R.id.ivdel3);
		ImageView ivdel4 = (ImageView) ly.findViewById(R.id.ivdel4);
		Button[] bts = { btBook1, btBook2, btBook3, btBook4 };
		ImageView[] ivdels = { ivdel1, ivdel2, ivdel3, ivdel4 };
		// ��ؼ���ֵ;
		for (int i = 0; i < bts.length; i++) {
			if ((position * bts.length) + i >= BookUtil.bookSize(books)) {
				break;
			}
			bts[i].setVisibility(View.VISIBLE);
			Book book = books.get(position * bts.length + i);
			if (book.imgpath != null && !"".equals(book.imgpath)) {
				Bitmap bitmap = BitmapFactory.decodeFile(book.imgpath);
				bts[i].setBackgroundDrawable(new BitmapDrawable(bitmap));
			} else {
				bts[i].setBackgroundDrawable(context.getResources()
						.getDrawable(R.drawable.def_cover_shell));
				bts[i].setText(books.get(position * bts.length + i).bookname);
			}
			if (context.isEdit) {
				ivdels[i].setVisibility(View.VISIBLE);
			} else {
				ivdels[i].setVisibility(View.GONE);
			}
			DelListener delListener = new DelListener(context, this, book);
			ivdels[i].setOnClickListener(delListener);
			context.ivDels.add(ivdels[i]);
			OpenBookListerer obListener = new OpenBookListerer(context, book.id);
			bts[i].setOnClickListener(obListener);
		}
		return ly;

	}

	/** ������ʾ */
	private View showInHorizontal(int position) {
		LinearLayout ly = (LinearLayout) inflater.inflate(
				R.layout.shelffloorhorizontal, null);
		// ��ø��ֿؼ�;
		Button btBook1 = (Button) ly.findViewById(R.id.btBook1);
		Button btBook2 = (Button) ly.findViewById(R.id.btBook2);
		Button btBook3 = (Button) ly.findViewById(R.id.btBook3);
		Button btBook4 = (Button) ly.findViewById(R.id.btBook4);
		Button btBook5 = (Button) ly.findViewById(R.id.btBook5);
		Button btBook6 = (Button) ly.findViewById(R.id.btBook6);
		Button btBook7 = (Button) ly.findViewById(R.id.btBook7);
		ImageView ivdel1 = (ImageView) ly.findViewById(R.id.ivdel1);
		ImageView ivdel2 = (ImageView) ly.findViewById(R.id.ivdel2);
		ImageView ivdel3 = (ImageView) ly.findViewById(R.id.ivdel3);
		ImageView ivdel4 = (ImageView) ly.findViewById(R.id.ivdel4);
		ImageView ivdel5 = (ImageView) ly.findViewById(R.id.ivdel5);
		ImageView ivdel6 = (ImageView) ly.findViewById(R.id.ivdel6);
		ImageView ivdel7 = (ImageView) ly.findViewById(R.id.ivdel7);
		Button[] bts = { btBook1, btBook2, btBook3, btBook4, btBook5, btBook6,
				btBook7 };
		ImageView[] ivdels = { ivdel1, ivdel2, ivdel3, ivdel4, ivdel5, ivdel6,
				ivdel7 };
		// ��ؼ���ֵ;
		for (int i = 0; i < bts.length; i++) {
			if ((position * bts.length) + i >= BookUtil.bookSize(books)) {
				break;
			}
			bts[i].setVisibility(View.VISIBLE);
			Book book = books.get(position * bts.length + i);
			if (book.imgpath != null && !"".equals(book.imgpath)) {
				Bitmap bitmap = BitmapFactory.decodeFile(book.imgpath);
				bts[i].setBackgroundDrawable(new BitmapDrawable(bitmap));
			} else {
				bts[i].setBackgroundDrawable(context.getResources()
						.getDrawable(R.drawable.def_cover_shell));
				bts[i].setText(books.get(position * bts.length + i).bookname);
			}
			if (context.isEdit) {
				ivdels[i].setVisibility(View.VISIBLE);
			} else {
				ivdels[i].setVisibility(View.GONE);
			}
			DelListener delListener = new DelListener(context, this, book);
			ivdels[i].setOnClickListener(delListener);
			context.ivDels.add(ivdels[i]);
			OpenBookListerer obListener = new OpenBookListerer(context, book.id);
			bts[i].setOnClickListener(obListener);
		}
		return ly;

	}
	

}
